package com.ys.filelib.filedialog.loader;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import com.ys.filelib.filedialog.ftp.FTPHelper;

import java.util.ArrayList;
import java.util.List;

public class RemoteDiskDao {
    public static final int TYPE_ALL = 0;
    public static final int TYPE_ONE_DRIVE = 5;
    public static final int TYPE_GOOGLE_DRIVE = 6;
    public static final int TYPE_FTP = 7;
    private static final String URI_QUERY = "content://com.ys.explorer.CloudDiskProvider/query";
    private static final String URI_INSERT = "content://com.ys.explorer.CloudDiskProvider/insert";
    private static final String URI_DELETE = "content://com.ys.explorer.CloudDiskProvider/delete";

    /**
     * 获取远程已经登陆的云端硬盘列表
     * @param type 云端硬盘类型
     */
    public static List<DriveUser> getDriveList(Context context, int type) {
        Uri uri = Uri.parse(URI_QUERY);
        ContentResolver resolver = context.getContentResolver();
        Cursor cursor = resolver.query(uri, null, null, null, null);
        List<DriveUser> list = new ArrayList<>();
        if (cursor == null) {
            return list;
        }
        String[] names = {"driveType", "userId", "email", "name", "path", "password", "port", "ip"};
        int[] pos = new int[names.length];
        for (int i = 0; i < pos.length; i++) {
            pos[i] = cursor.getColumnIndex(names[i]);
        }
        while (cursor.moveToNext()) {
            int driveType = cursor.getInt(pos[0]);
            if (type == TYPE_ALL || type == driveType) {
                DriveUser user = new DriveUser();
                user.driveType = driveType;
                user.userId = cursor.getString(pos[1]);
                user.email = cursor.getString(pos[2]);
                user.name = cursor.getString(pos[3]);
                user.path = cursor.getString(pos[4]);
                user.password = cursor.getString(pos[5]);
                user.port = cursor.getInt(pos[6]);
                user.ip = cursor.getString(pos[7]);
                if (user.path == null) {
                    user.path = "/";
                }
                list.add(user);
            }
        }
        cursor.close();
        return list;
    }

    /**
     *创建 DriveUser
     */
    public static DriveUser create(FTPHelper helper) {
        DriveUser user = new DriveUser();
        user.driveType = TYPE_FTP;
        user.port = helper.getPort();
        user.ip = helper.getHost();
        user.name = helper.getUserName();
        user.password = helper.getPassword();
        user.path = helper.getPath();
        if (user.path == null) {
            user.path = "/";
        }
        return user;
    }

    /**
     * 向远程增加新云端硬盘用户
     * @param user 云端硬盘用户
     */
    public static void insertDriveUser(Context context, DriveUser user) {
        Uri uri = Uri.parse(URI_INSERT);
        ContentResolver resolver = context.getContentResolver();
        ContentValues values = new ContentValues();
        values.put("driveType", user.driveType);
        values.put("userId", user.userId);
        values.put("email", user.email);
        values.put("name", user.name);
        values.put("path", user.path == null ? "/" : user.path);
        values.put("password", user.password);
        values.put("port", user.port);
        values.put("ip", user.ip);
        resolver.insert(uri, values);
    }

    /**
     * 删除远程云端硬盘用户
     */
    public static void deleteDriveUser(Context context, int driveType, String ip, int port, String username, String email) {
        Uri uri = Uri.parse(URI_DELETE);
        ContentResolver resolver = context.getContentResolver();
        String[] args = {driveType+"", email};
        if (driveType == TYPE_FTP) {
            args = new String[]{driveType+"", ip, port+"", username};
        }
        resolver.delete(uri, null, args);
    }

    public static class DriveUser {
        public int driveType;
        public String userId;
        public String email;
        public String name;
        public String path;
        public String password;
        public int port;
        public String ip;
        public String accessToken;
        public String refreshToken;

        public String photoUrl;
    }
}
